<?php
class Register extends CI_Controller {
    public function __construct() {
        parent::__construct();
        
        $this->load->model('user_model');
    }
    
    public function index() {
        $this->load->view('header');
        $this->load->view('navbar_index');
        $this->load->view('register');
        $this->load->view('footer');
    }
    
    public function do_register() {
        $email = $this->input->post('email');
        
        $ifExist = $this->user_model->exist($email);
        if ($ifExist === TRUE) {
            echo 'Your email has been used!';
        } else {
            $pw = $this->input->post('password');
            $salt = substr($pw, 32);
            $password = substr($pw, 0, 32);
            $nickname = substr($email, 0, strpos($email, '@'));
            
            $account = array(
                "email" => $email,
                "password" => $password,
                "salt" => $salt,
                "reg_time" => date('Y-m-d H:i:s'),
                "nickname" => $nickname
            );
            
            $res = $this->user_model->insert($account);
            if ($res === TRUE) {
                $this->load->helper('sendemail');
                sendVerifyEmail_helper($email);
                $this->load->view('header');
                $this->load->view('navbar_index');
                $this->load->view('post_output_test');
                $this->load->view('footer');
            } else {
                echo 'Error!';
            }
        }
    }
    
    public function ajax_register() {        //重写的使用Ajax的注册接口
        $data = array();
        $data['code'] = 0;
        
        $email = $this->input->post('email');
    
        $ifExist = $this->user_model->exist($email);
        if ($ifExist === TRUE) {
            $data['code'] = -1;
            $data['error_message'] = 'Your email has been used!';
        } else {
            $pw = $this->input->post('password');
            $salt = substr($pw, 32);
            $password = substr($pw, 0, 32);
            $nickname = substr($email, 0, strpos($email, '@'));
    
            $account = array(
                    "email" => $email,
                    "password" => $password,
                    "salt" => $salt,
                    "reg_time" => date('Y-m-d H:i:s'),
                    "nickname" => $nickname
            );
    
            $res = $this->user_model->insert($account);
            if ($res === TRUE) {
                $this->load->helper('sendemail');
                sendVerifyEmail_helper($email);
                $data['code'] = 1;
                $data['message'] = 'Register success';
            } else {
                $data['code'] = -99;
                $data['error_message'] = 'Unknown Error!';
            }
        }
        $this->output->set_content_type('application/json')
            ->set_output(json_encode($data));
    }
    
    public function register_success() {
        $this->load->view('header');
        $this->load->view('navbar_index');
        $this->load->view('post_output_test');
        $this->load->view('footer');
    }
}
?>